package xyz.yuzegod.megawalls.command;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.game.GamePlayer;

public class CommandCoins extends BaseCommand
{
    public CommandCoins() {
        super("coins");
        this.setPermission("MegaWalls.coins");
    }
    
    @Override
    public String getPossibleArguments() {
        return "give <玩家名> <数量>";
    }
    
    @Override
    public int getMinimumArguments() {
        return 0;
    }
    
    @Override
    public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
        if (args.length == 0 & !(sender instanceof ConsoleCommandSender)) {
            final Player p = (Player)sender;
            final GamePlayer gamePlayer = GamePlayer.get(p.getUniqueId());
            p.sendMessage("§a当前硬币: " + gamePlayer.getPlayerStats().getCoins());
        }
        else if (args[0].equalsIgnoreCase("give") && args.length >= 3) {
            final Player player = Bukkit.getPlayer(args[1]);
            int amount = 0;
            if (player == null) {
                sender.sendMessage("§a该玩家不在线!");
                return;
            }
            try {
                amount = Integer.valueOf(args[2]);
            }
            catch (NumberFormatException e) {
                sender.sendMessage("§c请输入有效的数量!");
                return;
            }
            if (amount <= 0) {
                sender.sendMessage("§c请输入有效的数量!");
                return;
            }
            final GamePlayer reciver = GamePlayer.get(player.getUniqueId());
            reciver.getPlayerStats().giveCoins(amount);
            sender.sendMessage("§a" + args[1] + "当前硬币: " + reciver.getPlayerStats().getCoins());
        }
    }
    
    @Override
    public boolean isOnlyPlayerExecutable() {
        return false;
    }
}
